package cn.iocoder.yudao.module.mzt.service.articleinteraction;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mzt.controller.admin.articleinteraction.vo.*;
import cn.iocoder.yudao.module.mzt.dal.dataobject.articleinteraction.ArticleInteractionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mzt.dal.mysql.articleinteraction.ArticleInteractionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mzt.enums.ErrorCodeConstants.*;

/**
 * 文章交互记录 Service 实现类
 *
 * @author Geordie
 */
@Service
@Validated
public class ArticleInteractionServiceImpl implements ArticleInteractionService {

    @Resource
    private ArticleInteractionMapper articleInteractionMapper;

    @Override
    public Long createArticleInteraction(ArticleInteractionSaveReqVO createReqVO) {
        // 插入
        ArticleInteractionDO articleInteraction = BeanUtils.toBean(createReqVO, ArticleInteractionDO.class);
        articleInteractionMapper.insert(articleInteraction);
        // 返回
        return articleInteraction.getId();
    }

    @Override
    public void updateArticleInteraction(ArticleInteractionSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleInteractionExists(updateReqVO.getId());
        // 更新
        ArticleInteractionDO updateObj = BeanUtils.toBean(updateReqVO, ArticleInteractionDO.class);
        articleInteractionMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticleInteraction(Long id) {
        // 校验存在
        validateArticleInteractionExists(id);
        // 删除
        articleInteractionMapper.deleteById(id);
    }

    private void validateArticleInteractionExists(Long id) {
        if (articleInteractionMapper.selectById(id) == null) {
            throw exception(ARTICLE_INTERACTION_NOT_EXISTS);
        }
    }

    @Override
    public ArticleInteractionDO getArticleInteraction(Long id) {
        return articleInteractionMapper.selectById(id);
    }

    @Override
    public PageResult<ArticleInteractionDO> getArticleInteractionPage(ArticleInteractionPageReqVO pageReqVO) {
        return articleInteractionMapper.selectPage(pageReqVO);
    }

}